package com.example.uupaymuch.controller.channel.eastpay;

import com.example.uupaymuch.controller.notice.east.EastPayComponent;
import com.example.uupaymuch.controller.notice.wudi.PayComponent;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Slf4j
@RestController
public class EastPayOrderController {

    @Autowired
    private EastPayComponent payComponent;

    private final Random random = new Random();
    private final double successProbability = 0.5; // 50% 的成功概率

    @PostMapping("/gateway/v1.0/payIn/create-bill")
    public ResponseEntity<Map<String, Object>> createTransaction(@RequestBody Map<String, Object> request) {
        // 生成一个随机数，并与成功概率进行比较
//        boolean isSuccess = random.nextDouble() < successProbability;

        // 生成一个随机数，并与成功概率进行比较
        boolean isSuccess = random.nextDouble() < successProbability;
        // 模拟入参
        String method = request.get("method") != null ? (String) request.get("method") : "";
        String partnerTrxId = request.get("partner_trx_id") != null ? (String) request.get("partner_trx_id") : "";
        String amount = request.get("amount") != null ? (String) request.get("amount") : "";
        String customerName = request.get("customer_name") != null ? (String) request.get("customer_name") : "";
        String email = request.get("email") != null ? (String) request.get("email") : "";
        String phone = request.get("phone") != null ? (String) request.get("phone") : "";
        String identityType = request.get("identity_type") != null ? (String) request.get("identity_type") : "";
        String identityNumber = request.get("identity_number") != null ? (String) request.get("identity_number") : "";
        String notifyUrl = request.get("notify_url") != null ? (String) request.get("notify_url") : "";
        String productDesc = request.get("product_desc") != null ? (String) request.get("product_desc") : "";


        Map<String, Object> response = new HashMap<>();
        String payStatusStr = "";

        // 构建数据部分
        Map<String, Object> data = new HashMap<>();
        data.put("amount", amount);
        data.put("plat_fee", 3000.00);
        data.put("partner_trx_id", partnerTrxId);
        data.put("plat_trx_id", "PB1745994650839289856");

        // 构建付款信息部分
        Map<String, String> payInfo = new HashMap<>();
        payInfo.put("method", "Checkout");
        payInfo.put("url", "https://www.baidu.com/");

        data.put("pay_info", payInfo);

        // 将数据部分添加到响应中
        response.put("data", data);
        response.put("code", "000");
        response.put("message", "Successful");

        payStatusStr = isSuccess ? "SUCCESS" : "FAILED";
        // 回调
        payComponent.doNotifyPay(partnerTrxId, payStatusStr, amount);

        // 立即返回响应
        return ResponseEntity.ok(response);
    }


    @PostMapping("/gateway/v1.0/access-token")
    public ResponseEntity<Map<String, Object>> authenticate(@RequestBody Map<String, Object> requestBody) {

        Map<String, Object> response = new HashMap<>();
        // 直接返回固定的响应
        response.put("code", "000");
        response.put("message", "Successful");
        response.put("access_token", "123");
        response.put("token_type", "Bearer");
        response.put("expires_in", 1800);

        return ResponseEntity.ok(response);
    }

//    @GetMapping("/api/charges/{txid}/simulate_payment")
//    public ResponseEntity<Map<String, Object>> authenticate(@PathVariable String txid) {
//
//        Map<String, Object> response = new HashMap<>();
//        response.put("message", "Payment performed successfully");
//
//        return ResponseEntity.ok(response);
//    }


//    @GetMapping("/api/notify")
//    public void notify(@RequestParam String ifCode,
//                       @RequestParam String wayCode,
//                       @RequestParam String payOrderId,
//                       @RequestParam Integer type) {
//        try {
//            payComponent.notifyPayment(ifCode, wayCode, payOrderId, type, "");
//        } catch (IOException e) {
//        }
//    }
}
